var User = require('../models/user')

exports.login =  function (req,res,next) {
    console.log(req.body)
    const reqUser = req.body.user
    const name = reqUser.name
    const password = reqUser.password
    User.findOne({name: name}, function(err, _user) {
        if (err) {
            console.log(err)
        }
        if (!user) {
            return res.redirect('/signup')
        }

        _user.comparePassword(password, function(err, isMatch) {
            if (err) {
                console.log(err)
            }

            if (isMatch) {

                return res.send({username: _user.name})
            }
            else {
                return res.redirect({username: null})
            }
        })
    })
}